import { useUserStore } from "@/stores";
import { createRouter, createWebHistory } from "vue-router";

// 导入创建路由器函数和Web历史模式的创建函数，并使用当前环境的基础URL初始化路由器
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录页面路由
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/LoginPage.vue"),
    },

    // 403禁止访问页面
    {
      path: "/403",
      name: "forbidden",
      component: () => import("@/views/forbidden/403.vue"),
    },
    // 404未找到页面，捕获所有未匹配的路由
    {
      path: "/:catchAll(.*)",
      name: "notFound",
      component: () => import("@/views/forbidden/404.vue"),
    },
    // 主页布局容器路由，重定向到数据统计页面
    {
      path: "/",
      name: "index",
      component: () => import("@/views/layout/LayoutContainer.vue"),
      meta: { requiresAdmin: true },
      redirect: "/dataStatistics/data",
      children: [
        // 数据统计页面
        {
          path: "/dataStatistics/data",
          name: "dataStatistics",
          component: () => import("@/views/dataStatistics/dataPage.vue"),
          meta: { title: "数据统计", icon: "el-icon-data-line" },
        },
        // 用户个人信息页面
        {
          path: "/user/profile",
          name: "userProfile",
          component: () => import("@/views/user/UserProfile.vue"),
          meta: { title: "用户个人信息", icon: "User" },
        },
      ],
    },
  ],
});

// 将子路由改为异步加载方式
export const asyncRoutes = [
  // 表情分类工具页面
  {
    path: "/tool/expressionClassify",
    name: "expressionClassify",
    component: () => import("@/views/tool/expressionClassify.vue"),
    meta: { title: "表情分类", icon: "Operation" },
  },
  // 表情管理器页面
  {
    path: "/tool/expressionManager",
    name: "expressionManager",
    component: () => import("@/views/tool/expressionManager.vue"),
    meta: { title: "表情管理", icon: "View" },
  },
  // 过滤器管理页面
  {
    path: "/tool/filterManager",
    name: "filterManager",
    component: () => import("@/views/tool/filterManager.vue"),
    meta: { title: "滤镜管理", icon: "MagicStick" },
  },
  // 菜单管理页面
  {
    path: "/authority/menuManager",
    name: "menuManager",
    component: () => import("@/views/authority/menuManager.vue"),
    meta: { title: "菜单管理", icon: "Menu" },
  },
  // 角色管理页面
  {
    path: "/authority/roleManager",
    name: "roleManager",
    component: () => import("@/views/authority/roleManager.vue"),
    meta: { title: "角色管理", icon: "Key" },
  },
  // 用户管理页面
  {
    path: "/authority/userManager",
    name: "userManager",
    component: () => import("@/views/authority/userManager.vue"),
    meta: { title: "用户管理", icon: "User" },
  },
  // 评论管理页面
  {
    path: "/comment/commentManager",
    name: "commentManager",
    component: () => import("@/views/comment/commentManager.vue"),
    meta: { title: "评论管理", icon: "ChatLineSquare" },
  },
  // 分享管理页面
  {
    path: "/share/shareManager",
    name: "shareManager",
    component: () => import("@/views/share/shareManager.vue"),
    meta: { title: "共享管理", icon: "Connection" },
  },
  // 日志管理页面
  {
    path: "/log/logManager",
    name: "logManager",
    component: () => import("@/views/log/logManager.vue"),
    meta: { title: "日志管理", icon: "CollectionTag" },
  },
  // 通知公告管理页面
  {
    path: "/notice/noticeManager",
    name: "noticeManager",
    component: () => import("@/views/notice/noticeManager.vue"),
    meta: { title: "公告管理", icon: "Bell" },
  },
];

// 全局前置守卫：登录拦截
router.beforeEach((to, from, next) => {
  // 判断是否需要用户认证
  if (to.path !== "/login" && !isLoggedIn()) {
    next("/login");
  } else {
    const userStore = useUserStore();
    userStore.handleFilteredRoutes();
    next();
  }
});

// 判断用户是否已登录
function isLoggedIn() {
  const userStore = useUserStore();
  if (!userStore.token) {
    return false;
  }
  if (userStore.token) {
    return true;
  }
}

export default router;
